/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.michalwski.mpnotepad;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 * @author Michal Piotrowski
 */
public class MPNotepad extends MIDlet implements CommandListener{
    private boolean midletPaused = false;

    private final Command exitCommand;
    private final Command searchFileSystemCommand;
    private final Command searchRecordStoreCommand;
    private final Command openNoteCommand;
    private final Command newNoteCommand;
    
    private Form startForm;
    private NoteEditor noteEditor;
    private TextBox noteTextBox;
    private final Command saveNoteCommand;
    private final Command backCommand;
    
    public MPNotepad() {
        exitCommand = new Command("Exit", Command.EXIT, 0);
        searchFileSystemCommand = new Command("Files", Command.ITEM, 0);
        searchRecordStoreCommand = new Command("Record Store", Command.ITEM, 0);
        openNoteCommand = new Command("Open", Command.OK, 0);
        newNoteCommand = new Command("New note", Command.CANCEL, 0);
        saveNoteCommand = new Command("Save", Command.OK, 0);
        backCommand = new Command("Back", Command.BACK, 0);
    }
    
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            startMIDlet ();
        }
        midletPaused = false;
    }
    
    public void pauseApp() {
        midletPaused = true;
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    private void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    private void resumeMIDlet() {
        
    }

    private void startMIDlet() {
        switchDisplayable(null, getStartScreen());
    }
    
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {                                            
        Display display = getDisplay();                                               
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }
    }

    private Display getDisplay() {
        return Display.getDisplay(this);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            exitMIDlet();
        } else if (c == newNoteCommand) {
            switchDisplayable(null, getTextBox(""));
        } else if (d == noteTextBox) {
            if (c == backCommand) {
                switchDisplayable(null, getStartScreen());
            }
        }
    }

    Form getStartScreen() {
        if (startForm == null) {
            startForm = new Form("MPNotepad");
            startForm.append("Create new note or edit existing one\n");
            startForm.addCommand(searchFileSystemCommand);
            startForm.addCommand(searchRecordStoreCommand);
            startForm.addCommand(newNoteCommand);
            startForm.addCommand(exitCommand);
            startForm.setCommandListener(this);
        }
        
        return startForm;
    }

    private Displayable getNoteEditor() {
        if (noteEditor == null) {
            noteEditor = new NoteEditor(this);
        }
        
        return noteEditor;
    }

    private Displayable getTextBox(String content) {
        noteTextBox = new NoteEditor(this);
        return noteTextBox;
        
    }

    
}
